package com.megvii.corelib.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.megvii.corelib.R;
import com.megvii.corelib.components.BaseNetworkErrorView;
import com.megvii.corelib.components.NetworkErrorView;
import com.megvii.corelib.components.ProgressBarView;

import java.io.Serializable;

import butterknife.ButterKnife;

/**
 * Created by linyue on 15/12/22.
 */
public abstract class MegviiActivity<PRESENTER extends MegviiPresenter, PARAM extends Serializable>
        extends Activity implements PresenterHolder<PRESENTER>, NetworkErrorView.ButtonListener
{
    public static final String KEY_ACTIVITY_PARAM = "MEGVII_ACTIVITY_PARAM";

    protected PRESENTER presenter;

    protected PARAM activityParam;

    protected ProgressBarView progressBar;

    protected BaseNetworkErrorView networkErrorView;

    private boolean created = false;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        activityParam = (PARAM) getIntent().getSerializableExtra(KEY_ACTIVITY_PARAM);
        if (presenter == null)
        {
            presenter = MegviiPresenter.buildPresenter(this.getPresenterClass());
            presenter.doInit(this);
        }
    }

    @Override
    public void setContentView(int layoutResID)
    {
        super.setContentView(layoutResID);
        init();
    }

    @Override
    public void setContentView(View view)
    {
        super.setContentView(view);
        init();
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params)
    {
        super.setContentView(view, params);
        init();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        if (!created)
        {
            presenter.onCreated();
            created = true;
        }
        presenter.onResume();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        presenter.onPause();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        presenter.onDestory();
    }

    private void init()
    {
        if (autoButterKnifeBind())
        {
            ButterKnife.bind(this);
        }
        buildProgressBar();
        buildNetworkErrorLayout();
    }

    /**
     * 如果子类不要将views绑定到自己中，则重载这个函数，返回false
     * @return
     */
    protected boolean autoButterKnifeBind()
    {
        return true;
    }

    private void buildProgressBar()
    {
        if (progressBar == null)
        {
            progressBar = new ProgressBarView(this);
            //将progress bar加到了跟content view同级的位置
            View view = this.findViewById(android.R.id.content);
            if (view instanceof ViewGroup)
            {
                ((ViewGroup) view).addView(progressBar);
            }
        }
    }

    private void buildNetworkErrorLayout()
    {
        networkErrorView = getNetworkErrorView();
        if (networkErrorView != null)
        {
            networkErrorView.setButtonListener(this);
        }
    }

    /**
     * @param destinationClass
     * @param destinationParam
     */
    public void startActivity(Class<?> destinationClass, Serializable destinationParam)
    {
        Intent intent = new Intent(this, destinationClass);
        if (destinationParam != null)
        {
            intent.putExtra(KEY_ACTIVITY_PARAM, destinationParam);
        }
        startActivity(intent);
    }

    /**
     * @param destinationClass
     * @param destinationParam
     */
    public void startActivityForResult(Class<?> destinationClass, Serializable destinationParam,
            int requestCode)
    {
        Intent intent = new Intent(this, destinationClass);
        if (destinationParam != null)
        {
            intent.putExtra(KEY_ACTIVITY_PARAM, destinationParam);
        }
        startActivityForResult(intent, requestCode);
    }

    public PARAM getActivityParam()
    {
        return activityParam;
    }

    @Override
    public void finish()
    {
        super.finish();
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
    }

    @Override
    public void showProgressBar()
    {
        progressBar.show();
    }

    @Override
    public void hideProgressBar()
    {
        progressBar.hide();
    }

    @Override
    public Context getContextX()
    {
        return this;
    }

    @Override
    public void showNetworkError()
    {
        if (networkErrorView != null)
        {
            networkErrorView.show();
        }
    }

    @Override
    public void hideNetworkError()
    {
        if (networkErrorView != null)
        {
            networkErrorView.hide();
        }
    }

    @Override
    public void onNetworkErrorButtonClick()
    {
        presenter.onClickRefreshNetwork();
    }
}
